<?php
/*
 * Copyright 2008 by Oliver Radwan, Maxwell Palmer, Nolan McNair,
 * Taylor Talmage, and Allen Tucker.  This program is part of RMH Homebase.
 * RMH Homebase is free software.  It comes with absolutely no warranty.
 * You can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
*/

/**
 * Functions to create, update, and retrieve information from the
 * dbSCL table in the database.  This table is used with the SCL
 * class.  Sub Call Lists are generated by editShift and viewed and edited
 * with subCallList.php
 * @version May 1, 2008
 * @author Maxwell Palmer
 */

include_once('dbinfo.php');
include_once('SCL.php');


 /**
  * Sets up a new dbSCL table by dropping and recreating
  * id - same as corresponding shift id
  * persons - array
  * status - open/closed
  * vacancies - # of openings to fill
  * time - used to sort sub call lists properly
  */
function setup_dbSCL(){
	connect();
	mysql_query("DROP TABLE IF EXISTS dbSCL");
	//NOTE: primary key set to id.  id is text in the form: mm-dd-yy-ss-se,  ss=shift start,  se=shift end
	$result=mysql_query("CREATE TABLE dbSCL (id CHAR(17) NOT NULL,persons TEXT, status TEXT, vacancies TEXT, time TEXT, PRIMARY KEY(id))");
	if(!$result)
		echo mysql_error();
	mysql_close();
}

/**
 * adds a SCL to the table
 */
function insert_dbSCL($scl){
	if (! $scl instanceof SCL) {
		die ("Invalid argument for dbSCL->insert_dbSCL($) function call");
	}
	$persons=$scl->get_persons();
	if($persons)  {
		for($i=0;$i<count($persons);++$i) {
			$persons[$i]=implode("+",$persons[$i]);
			//echo $i.":\t";
			//print_r($persons[$i]);
		}
		$persons=implode(",",$persons);
	}
	connect();
	$query = "INSERT INTO dbSCL VALUES
				(\"".$scl->get_id()."\",\"".$persons."\",\"".$scl->get_status()."\",\"".$scl->get_vacancies()."\",\"".$scl->get_time()."\")";
	$result=mysql_query($query);
	if(!$result){
		echo "unable to insert into dbSCL: ".$scl->get_id().mysql_error();
	    mysql_close();
	    return false;
	}
	mysql_close();
	return true;
}

/**
 * deletes SCl from db
 */
function delete_dbSCL($scl){
	if (! $scl instanceof SCL)
		die ("Invalid argument for dbSCL->delete_dbSCL($scl) function call");
	connect();
	$query="DELETE FROM dbSCL WHERE id=\"".$scl->get_id()."\"";
	$result=mysql_query($query);
	if(!$result) {
		echo "unable to delete dbSCL: ".$scl->get_id().mysql_error();
        mysql_close();
        return false;
    }
    mysql_close();
    return true;
}
/**
 * updates a SCL in the database by deleting it and re-inserting it.
 */
 function update_dbSCL($scl){
	if (! $scl instanceof SCL)
		die ("Invalid argument for dbSCL->update_dbSCL($) function call");
	delete_dbSCL($scl);      // try to delete
	if (insert_dbSCL($scl))  // and then insert
	    return true;
    else return false;
}

/**
 * @return returns a SCL object, or an error string if the SubCallList is not in the database
 */
function select_dbSCL($id){
	connect();
	$query="SELECT * FROM dbSCL WHERE id=\"".$id."\"";
	$result=mysql_query($query);
	mysql_close();
	if(!$result){
		echo "Entry ".$id." is not in the database. ".mysql_error();
		return null;
	}
	else{
		$result_row=mysql_fetch_row($result);
		if($result_row) {
			$persons=explode(",",$result_row[1]);
			for($i=0;$i<count($persons);++$i) {
				$persons[$i]=explode("+",$persons[$i]);
			}
			if(!$persons[0][0])
				$persons=null;
			return new SCL($result_row[0],$persons,$result_row[2],$result_row[3],$result_row[4]);
		}
		return null;
	}
}

?>
